package com.wangmei.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangmei.model.Transport;
import com.wangmei.service.TransportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;
import unitTool.Dictionary;
import unitTool.JsonRsp;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/transport")
public class TransportController {

    @Autowired
    private TransportService transportService;

    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public JsonRsp transport(
            @RequestParam(value = "from", required = false) String from
            , @RequestParam(value = "to", required = false) String to
            , @RequestParam(value = "search", required = false) String key
            , @RequestParam(value = "pageIndex", defaultValue = Dictionary.PageDirect.PAGE_INDEX)
                    int pageIndex
            , @RequestParam(value = "pageSize", defaultValue = Dictionary.PageDirect.PAGE_SIZE)
                    int pageSize) {
        try {
            Map<String, Object> map = new HashMap<>();
            if (from != null)
                map.put("from", from);
            if (to != null)
                map.put("to", to);
            if (key != null) {
//                System.out.println(key);
//                key =  new String(key.getBytes("ISO8859-1"),"utf-8");
//                System.out.println(key);
                map.put("key", key);
            }
            PageHelper.startPage(pageIndex, pageSize);
            PageInfo page = new PageInfo(transportService.queryAll(map));
            return JsonRsp.success(page.getList()).setTotal(page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonRsp.fail(4000, "内部异常");
        }

    }

    @RequestMapping(value = "/addTran", method = RequestMethod.POST)
    public JsonRsp addTransport(@RequestBody Transport transport) {
        try {
            return JsonRsp.success(transportService.insert(transport));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonRsp.fail(4000, "内部异常");
        }
    }

    @RequestMapping(value = "/deleteTran", method = RequestMethod.POST)
    public JsonRsp delete(@RequestParam(value = "tid") int tid) {
        try {
            return JsonRsp.success(transportService.delete(tid));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonRsp.fail(4000, "内部异常");
        }
    }

    @RequestMapping(value = "/updateTran", method = RequestMethod.POST)
    public JsonRsp updateTran(@RequestBody Transport transport) {
        try{
            return JsonRsp.success(transportService.update(transport));
        }catch (Exception e){
            e.printStackTrace();
            return JsonRsp.fail(400,"内部异常");
        }
    }
}
